package gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import physics.Location;
import physics.Vector;

public class VectorModifier extends JComponent {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private VectorModifier thiz = this;
	private Vector mine;
	private Vector ret;
	private NumberInput magnitude;
	private MovableArrow direction;
	public VectorModifier(Vector val) {
		mine = val.unit();
		setLayout(new FlowLayout());
		magnitude = new NumberInput("Magnitude:",val.getNetMag());
		direction = new MovableArrow();
		add(magnitude);
		add(direction);
	}
	class MovableArrow extends JComponent implements MouseMotionListener {
		public MovableArrow() {
			addMouseMotionListener(this);
		}
		@Override
		public void paint(Graphics g) {
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, getWidth(), getHeight());
			g.setColor(Color.BLACK);
			g.fillOval(getWidth()/2-5, getHeight()/2-5, 10, 10);
			GraphicsUtil.drawArrow(g,getWidth()/2,getHeight()/2,(int)(getWidth()/2+mine.unit().getXMag()*getWidth()/2),(int)(getHeight()/2-mine.unit().getYMag()*getHeight()/2));
		}
		public void mouseDragged(MouseEvent me) {
			double x = ((double)(me.getX() - getWidth()/2)*2)/((double)getWidth());
			double y = ((double)(getHeight()/2-me.getY())*2)/((double)getHeight());
			mine = new Vector(mine.getLocation(),mine.getType(),x,y).unit();
			repaint();
		}
		public void mouseMoved(MouseEvent me) {
			// TODO Auto-generated method stub
		}
		public Dimension getPreferredSize() {
			return new Dimension(100,100);
		}
	}
	public Vector getVector() {
		return mine.multiply(magnitude.getNum());
	}
}
